/**
 * @author 徐楠
 * @date 2022/5/2 16:00
 * @version 1.0
 */

package com.xunan.likou;

import java.util.Arrays;
import java.util.List;

public class MinimumTimeDifference {
    public static void main(String[] args) {
        List<String> timePoints = List.of(new String[]{"23:59", "00:00"});
        int minDifference = findMinDifference1(timePoints);
        System.out.println(minDifference);
    }

    public static int findMinDifference(List<String> timePoints) {
        int min = Integer.MAX_VALUE;
        String last = timePoints.get(0);
        for (int i = 1; i < timePoints.size(); i++) {
            String next = timePoints.get(i);
            String[] splitNext = next.split(":");
            String[] splitLast = last.split(":");
            int H = Math.abs(Integer.valueOf(splitNext[0]) - Integer.valueOf(splitLast[0])) * 60;
            int M = Math.abs(Integer.valueOf(splitNext[1]) - Integer.valueOf(splitLast[1]));
            min = Math.min(min, H + M);
        }
        return min;
    }

    public static int findMinDifference1(List<String> timePoints) {
        int TOTAL = 24 * 60;
        if (timePoints.size() > TOTAL) {
            return 0;
        }
        int[] nums = new int[timePoints.size()];
        int minTime = TOTAL * 2;
        for (int i = 0; i < nums.length; i++) {
            String time = timePoints.get(i);
            String[] split = time.split(":");
            int h = Integer.parseInt(split[0]);
            int m = Integer.parseInt(split[1]);
            nums[i] = h * 60 + m;
            minTime = Math.min(minTime, nums[i] + TOTAL);
        }
        Arrays.sort(nums);
        int answer = minTime - nums[nums.length - 1];
        for (int i = 0; i < nums.length - 1; i++)
            answer = Math.min(answer, nums[i + 1] - nums[i]);
        return answer;
    }

}
